﻿using System;
using System.Collections.Generic;
using System.Text;

namespace PIM_ATE
{
    class JcPIMBandInfomation
    {//记录默认频段信息，频段范围信息
        public double F1s { get; private set; }
        public double F1e { get; private set; }
        public double F2s { get; private set; }
        public double F2e { get; private set; }
        public double RXs { get; private set; }
        public double RXe { get; private set; }
        public double Step { get; private set; }

        public double range_f1_min { get; private set; }
        public double range_f1_max { get; private set; }
        public double range_f2_min { get; private set; }
        public double range_f2_max { get; private set; }

        public double range_rx_start { get; private set; }
        public double range_rx_end { get; private set; }

        double tx_min = 30;
        public double range_tx_min { get { return tx_min; } }
        double tx_max = 60;
        public double range_tx_max { get { return tx_max; } }

        public string range_name { get; private set; }
        public string range_alias { get; private set; }
        public string ini_path { get; private set; }

        public JcPIMBandInfomation(string path, string bandName)
        {
            this.range_name = bandName;
            this.ini_path = path;
        }

        public void GetDefaultConfig()
        {
            IniFile.SetFileName(this.ini_path);
            range_alias = IniFile.IniReadValue(range_name, "alias", "");
            F1s = Convert.ToSingle(IniFile.IniReadValue(range_name, "default_f1s", "0"));
            F1e = Convert.ToSingle(IniFile.IniReadValue(range_name, "default_f1e", "0"));
            F2s = Convert.ToSingle(IniFile.IniReadValue(range_name, "default_f2s", "0"));
            F2e = Convert.ToSingle(IniFile.IniReadValue(range_name, "default_f2e", "0"));
            RXs = Convert.ToSingle(IniFile.IniReadValue(range_name, "default_rx_start", "0"));
            RXe = Convert.ToSingle(IniFile.IniReadValue(range_name, "default_rx_end", "0"));
            Step = Convert.ToSingle(IniFile.IniReadValue(range_name, "default_step", "0"));
        }

        public void GetRangeConfig()
        {
            IniFile.SetFileName(this.ini_path);
            range_f1_min = Convert.ToSingle(IniFile.IniReadValue(range_name, "range_f1s", "0"));
            range_f1_max = Convert.ToSingle(IniFile.IniReadValue(range_name, "range_f1e", "0"));
            range_f2_min = Convert.ToSingle(IniFile.IniReadValue(range_name, "range_f2s", "0"));
            range_f2_max = Convert.ToSingle(IniFile.IniReadValue(range_name, "range_f2e", "0"));
            range_rx_start = Convert.ToSingle(IniFile.IniReadValue(range_name, "range_rx_start", "0"));
            range_rx_end = Convert.ToSingle(IniFile.IniReadValue(range_name, "range_rx_end", "0"));
        }
    }

    class JcPIMInstrumentInfo
    {//记录仪表信息
        public bool instrEnable { get; set; }
        //仪表类型
        public Instrument instrType { get;  set; }
        //仪表别名
        public string instrBandAlias { get; set; }
        //仪表别名
        public string instrBandName { get; set; }
        //频段序号
        public int instrBandIndex { get; set; }
        //仪表地址
        public string instrAddress { get;  set; }
        //仪表端口数量
        public int instrPort { get; set; }

        public JcPIMInstrumentInfo(bool enable, string name, string address, Instrument type)
        {
            instrBandAlias = name;
            instrAddress = address;
            instrType = type;
            instrEnable = enable;

            if (instrType == Instrument.DESKTOP_FOR_FWD)
                instrPort = 1;
            else if (instrType == Instrument.DESKTOP_FOR_1REV)
                instrPort = 1;
            else if (instrType == Instrument.DESKTOP_FOR_2REV)
                instrPort = 2;
            else if (instrType == Instrument.DESKTOP_FOR_4REV)
                instrPort = 4;
            else if (instrType == Instrument.DESKTOP_FOR_6REV)
                instrPort = 6;
            else if (instrType == Instrument.DESKTOP_FOR_PORT)
                instrPort = 2;
            else if (instrType == Instrument.MULTI_BAND_FOR_HEIWEI)
                instrPort = 2;
            else if (instrType == Instrument.MULTI_BAND_FOR_HEIWEI_FWD)
                instrPort = 1;
            else if (instrType == Instrument.MULTI_BAND_FOR_POI)
                instrPort = 1;
            else if (instrType == Instrument.MULTI_BAND_FOR_NEWPOI)
                instrPort = 2;
            else if (instrType == Instrument.MULTI_BAND_FOR_NEWHEIWEI)
                instrPort = 2;
            else
                instrPort = 1;
        }
    }

    class JcPIMDevicesBuilder
    {//频段信息builder
        /// <summary>
        /// 别名-实名
        /// </summary>
        public static Dictionary<string, JcPIMBandInfomation> dicBands = null;
        /// <summary>
        /// 实名-仪表
        /// </summary>
        public static Dictionary<string, JcPIMInstrumentInfo> dicDevices = null;

        public static void LoadDefaultBand(string path)
        {
            dicDevices = new Dictionary<string, JcPIMInstrumentInfo>();
            dicBands = new Dictionary<string, JcPIMBandInfomation>();
            var lstBandName = new List<string>();
            lstBandName.Add("JOINTCOM-700-H");
            lstBandName.Add("JOINTCOM-700-L");
            lstBandName.Add("JOINTCOM-800");
            lstBandName.Add("JOINTCOM-DD800");
            lstBandName.Add("JOINTCOM-900");
            lstBandName.Add("JOINTCOM-1800");
            lstBandName.Add("JOINTCOM-1900");
            lstBandName.Add("JOINTCOM-2100");
            lstBandName.Add("JOINTCOM-2600");
            lstBandName.Add("JOINTCOM-MBD-V");
            lstBandName.Add("JOINTCOM-LBD-V");
            lstBandName.Add("JOINTCOM-400");
            lstBandName.Add("Rflight-700-1000");
            lstBandName.Add("Rflight-1800");

            IniFile.SetFileName(path);
            for (int i = 0; i < lstBandName.Count; i++)
            {
                JcPIMBandInfomation info = new JcPIMBandInfomation(path, lstBandName[i]);
                info.GetDefaultConfig();
                info.GetRangeConfig();
                if (info.range_alias == "") continue;
                dicBands.Add(info.range_alias, info);
            }
        }

        public static JcPIMInstrumentInfo[] LoadDesktopInstrument(string start_path)
        {
            JcPIMInstrumentInfo[] lstInstruments = null;
            IniFile.SetFileName(start_path + "\\JcDevices.ini");
            string section = "Desktop_Config";

            int count = Convert.ToInt32(IniFile.IniReadValue(section, "Device_Count", "0"));
            if (count <= 0) throw new Exception("JcDevice配置设备数量为0，请重新设置");
            if (GDefine.RUNNING_MODE == 1) count = 1;

            lstInstruments = new JcPIMInstrumentInfo[count];
            for (int i = 1; i <= count; i++)
            {
                //获取别名
                string band = IniFile.IniReadValue(section, "Device" + i + "_Band", "0");
                bool enable = IniFile.IniReadValue(section, "Device" + i + "_Enable", "1") == "1";
                //获取仪表类型
                Instrument type;
                switch (IniFile.IniReadValue(section, "Device" + i + "_Type", "0"))
                {
                    case "DESKTOP_FOR_1REV": type = Instrument.DESKTOP_FOR_1REV; break;
                    case "DESKTOP_FOR_2REV": type = Instrument.DESKTOP_FOR_2REV; break;
                    case "DESKTOP_FOR_4REV": type = Instrument.DESKTOP_FOR_4REV; break;
                    case "DESKTOP_FOR_6REV": type = Instrument.DESKTOP_FOR_6REV; break;
                    case "DESKTOP_FOR_PORT": type = Instrument.DESKTOP_FOR_PORT; break;
                    case "DESKTOP": type = Instrument.DESKTOP; break;
                    default: throw new Exception("JcDevice文件格式不正确");
                }
                //获取地址
                string ip = IniFile.IniReadValue(section, "Device" + i + "_IP", "127.0.0.1");
                //初始化实例
                lstInstruments[i - 1] = new JcPIMInstrumentInfo(enable, band, ip, type);
                lstInstruments[i - 1].instrBandName = dicBands[band].range_name;
                lstInstruments[i - 1].instrBandIndex = -1;
                dicDevices.Add(dicBands[band].range_name, lstInstruments[i - 1]);
            }

            return lstInstruments;
        }

        public static JcPIMInstrumentInfo[] LoadMultiBandInstrument(string start_path)
        {
            JcPIMInstrumentInfo[] lstInstruments = null;
            IniFile.SetFileName(start_path + "\\JcConfig.ini");

            string address = "";
            address += IniFile.IniReadValue("Settings", "signal1", "0") + ",";
            address += IniFile.IniReadValue("Settings", "signal2", "0") + ",";
            address += IniFile.IniReadValue("Settings", "analyzer", "0") + ",";
            address += IniFile.IniReadValue("Settings", "sensor", "0") + ",";
            address += "1";

            Instrument type;
            switch (Convert.ToInt32(IniFile.IniReadValue("Settings", "type_trans", "0")))
            {
                case 0: type = Instrument.MULTI_BAND_FOR_HEIWEI; break;
                case 1: type = Instrument.MULTI_BAND_FOR_HEIWEI_FWD; break;
                case 2: type = Instrument.MULTI_BAND_FOR_POI; break;
                case 3: type = Instrument.MULTI_BAND_FOR_NEWPOI; break;
                case 4: type = Instrument.MULTI_BAND_FOR_NEWHEIWEI; break;
                default: throw new Exception("JcConfig文件不正确");
            }

            #region 柜式仪表默认配置

            lstInstruments = new JcPIMInstrumentInfo[7];
            lstInstruments[0] = new JcPIMInstrumentInfo(true, "800", address, type);
            lstInstruments[0].instrBandName = "JOINTCOM-800";
            lstInstruments[0].instrBandIndex = 0;
            dicDevices.Add(lstInstruments[0].instrBandName, lstInstruments[0]);

            lstInstruments[1] = new JcPIMInstrumentInfo(true, "900", address, type);
            lstInstruments[1].instrBandName = "JOINTCOM-900";
            lstInstruments[1].instrBandIndex = 1;
            dicDevices.Add(lstInstruments[1].instrBandName, lstInstruments[1]);

            lstInstruments[2] = new JcPIMInstrumentInfo(true, "1800", address, type);
            lstInstruments[2].instrBandName = "JOINTCOM-1800";
            lstInstruments[2].instrBandIndex = 2;
            dicDevices.Add(lstInstruments[2].instrBandName, lstInstruments[2]);

            lstInstruments[3] = new JcPIMInstrumentInfo(true, "1900", address, type);
            lstInstruments[3].instrBandName = "JOINTCOM-1900";
            lstInstruments[3].instrBandIndex = 3;
            dicDevices.Add(lstInstruments[3].instrBandName, lstInstruments[3]);

            lstInstruments[4] = new JcPIMInstrumentInfo(true, "2100", address, type);
            lstInstruments[4].instrBandName = "JOINTCOM-2100";
            lstInstruments[4].instrBandIndex = 4;
            dicDevices.Add(lstInstruments[4].instrBandName, lstInstruments[4]);

            lstInstruments[5] = new JcPIMInstrumentInfo(true, "2600", address, type);
            lstInstruments[5].instrBandName = "JOINTCOM-2600";
            lstInstruments[5].instrBandIndex = 5;
            dicDevices.Add(lstInstruments[5].instrBandName, lstInstruments[5]);

            lstInstruments[6] = new JcPIMInstrumentInfo(true, "700", address, type);
            lstInstruments[6].instrBandName = "JOINTCOM-700-L";
            lstInstruments[6].instrBandIndex = 6;
            dicDevices.Add(lstInstruments[6].instrBandName, lstInstruments[6]);

            #endregion

            return lstInstruments;
        }
    }

}
